var tgd = tgd || {};
tgd.Simulations = tgd.Simulations || {};
tgd.Simulations.Zombies = tgd.Simulations.Zombies || {};

tgd.Simulations.Zombies.HunterBrain = function (physics, vision, id) {

	var _p = physics;
	var _v = vision;
	var _nextHorizDir = tgd.Simulations.Zombies.Enums.Direction.East;
	var _nextVertiDir = tgd.Simulations.Zombies.Enums.Direction.South;
	var _numSteps = -1;
	var _i = id;

	//this.ID getter = function() { return _i; }
	this.ID = _i;

	// @return { Type, Position? : { X, Y } /* move */, Person /* attack */ }
	this.think = function() {
		var retVal = {};
		var results = _v.getVision();
		var attackable = _p.getPeopleInAttackRange(this);

		if (attackable.Zombies.length > 0) {
			retVal.Person = attackable.Zombies[0];
			retVal.Type = tgd.Simulations.Zombies.Enums.ActionType.Attack;
		} else if (results.Zombies.length > 0) {
			retVal.Position = {X : results.Zombies[0].Position.X, Y : results.Zombies[0].Position.Y};
			retVal.Type = tgd.Simulations.Zombies.Enums.ActionType.Move;
		} else {  //todo: look smarter, i.e. hunt!!!
			if (--_numSteps > 0) {  //move vertical
				retVal.Position = { X : 0, Y : (_nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.North ? -1 : 1) };
				if ((results.Walls.North !== undefined && _nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.North) || (results.Walls.South !== undefined && _nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.South)) {
					_numSteps = 0;
					_nextVertiDir = (_nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.North ? tgd.Simulations.Zombies.Enums.Direction.South : tgd.Simulations.Zombies.Enums.Direction.North);
				}
			} else if (_nextHorizDir === tgd.Simulations.Zombies.Enums.Direction.East && results.Walls.East === undefined) {
				retVal.Position = { X : 1, Y : 0 };
			} else if (_nextHorizDir === tgd.Simulations.Zombies.Enums.Direction.West && results.Walls.West === undefined) {
				retVal.Position = { X : -1, Y : 0 };
			} else if (_nextHorizDir === tgd.Simulations.Zombies.Enums.Direction.East && results.Walls.East !== undefined) {
				_numSteps = _v.FieldOfView * 2;
				retVal.Position = { X : 0, Y : (_nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.North ? -1 : 1) };
				_nextHorizDir = tgd.Simulations.Zombies.Enums.Direction.West;
			} else if (_nextHorizDir === tgd.Simulations.Zombies.Enums.Direction.West && results.Walls.West !== undefined) {
				_numSteps = _v.FieldOfView * 2;
				retVal.Position = { X : 0, Y : (_nextVertiDir === tgd.Simulations.Zombies.Enums.Direction.North ? -1 : 1) };
				_nextHorizDir = tgd.Simulations.Zombies.Enums.Direction.East;
			} else {
				retVal.Position = {X : parseInt(Math.random() * 10) - 5, Y : parseInt(Math.random() * 10) - 5};
			}
			retVal.Type = tgd.Simulations.Zombies.Enums.ActionType.Move;
		}

		return retVal;
	}
}
